package com.guli.mall.pms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.mall.pms.entity.SpuInfoEntity;
import com.guli.mall.pms.entity.vo.SkuInfoWithMinMaxPriceVo;
import com.guli.mall.pms.service.SkuImagesService;
import com.guli.mall.pms.service.SpuInfoService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.pms.mapper.SkuInfoMapper;
import com.guli.mall.pms.entity.SkuInfoEntity;
import com.guli.mall.pms.service.SkuInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfoEntity> implements SkuInfoService {

    @Resource
    SkuImagesService skuImagesService;

    @Resource
    SkuInfoService skuInfoService;

    @Resource
    AliOssServiceImpl aliOssService;

    @Override
    public PageUtils queryPage(Integer currentPage, Integer size) {

        Page<SkuInfoEntity> page = new Page<>(currentPage, size);

        Page<SkuInfoEntity> resultPage = this.page(page);


        return new PageUtils(resultPage);
    }

    @Override
    public PageUtils<SkuInfoEntity> queryPageByCondition(Integer currentPage, Integer size, SkuInfoWithMinMaxPriceVo skuInfoWithMinMaxPriceVo) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getMinPrice())) {
            wrapper.ge("price", skuInfoWithMinMaxPriceVo.getMinPrice());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getMaxPrice())) {
            wrapper.le("price", skuInfoWithMinMaxPriceVo.getMaxPrice());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getBrandId())) {
            wrapper.eq("brand_id", skuInfoWithMinMaxPriceVo.getBrandId());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getCatalogId())) {
            wrapper.eq("catalog_id", skuInfoWithMinMaxPriceVo.getCatalogId());
        }

        if (!StringUtils.isEmpty(skuInfoWithMinMaxPriceVo.getSkuName())) {
            wrapper.like("sku_name", skuInfoWithMinMaxPriceVo.getSkuName());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getSkuId())) {
            wrapper.eq("sku_id", skuInfoWithMinMaxPriceVo.getSkuId());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getSpuId())) {
            wrapper.eq("spu_id", skuInfoWithMinMaxPriceVo.getSpuId());
        }

        if (!ObjectUtils.isEmpty(skuInfoWithMinMaxPriceVo.getId())) {
            wrapper.or(w -> {
                w.eq("sku_id",skuInfoWithMinMaxPriceVo.getId());
            }).or(w->{
                w.eq("spu_id",skuInfoWithMinMaxPriceVo.getId());
            });
        }

        Page<SkuInfoEntity> skuInfoEntityPage = new Page<>(currentPage, size);

        Page<SkuInfoEntity> page = this.page(skuInfoEntityPage, wrapper);

        return new PageUtils<>(page);
    }

    @Override
    public List<SkuInfoEntity> getListBySpuId(Long id) {

        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();

        skuInfoEntityQueryWrapper.eq("spu_id", id);

        String aliOssImgPrefix = aliOssService.getAliOssImgPrefix();

        List<SkuInfoEntity> list = this.list(skuInfoEntityQueryWrapper);

        for (SkuInfoEntity skuInfoEntity : list) {
            if (skuInfoEntity.getSkuDefaultImg().startsWith("20")) {
                skuInfoEntity.setSkuDefaultImg(aliOssImgPrefix + skuInfoEntity.getSkuDefaultImg());
            }
            String[] split = skuInfoEntity.getSkuDesc().split(",");
            List<String> collect = Arrays.stream(split).map(desc -> {
                return aliOssImgPrefix + desc;
            }).collect(Collectors.toList());

            String s = StringUtils.collectionToDelimitedString(collect, ",");

            skuInfoEntity.setSkuDesc(s);
        }

        return list;

    }

    @Override
    public SkuInfoEntity getByIdAndAddImgPredix(Long skuId) {

        SkuInfoEntity byId = this.getById(skuId);

        byId.setSkuDefaultImg(skuImagesService.getImagePrefix() + byId.getSkuDefaultImg());

        return byId;
    }

    @Override
    public List<SkuInfoEntity> getSkuListBySpuIdAndAddImgPredix(Long spuId) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("spu_id", spuId);

        List<SkuInfoEntity> list = this.list(wrapper);

        list.forEach(skuInfo -> {
            skuInfo.setSkuDefaultImg(skuImagesService.getImagePrefix() + skuInfo.getSkuDefaultImg());
        });

        return list;
    }


}